package com.foreknow.book;

import com.sun.org.apache.xpath.internal.operations.Or;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Order {
    private String orderId;// 订单的编号
    private double total;// 订单总额
    private List<OrderItem> items;// 保存多个订单项，所购买的每个商品就是一个OrderItem对象（图书的名称，单价，购买数量）

    public Order(String orderId) {
        this.orderId = orderId;
        this.items = new ArrayList<>();
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }



    public double getTotal() {
        // 调用calTotal()方法计算订单的总额
        calTotal();
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public List<OrderItem> getItems() {
        return items;
    }

    public void setItems(OrderItem[] items) {
        this.items = Arrays.asList(items);
    }

    /**
     * 将订单项OrderItem对象添加到数组中,i代表数组中的下标
     */
    public void setItem(OrderItem item) {
        this.items.add(item);
    }


    /**
     * 计算订单的总额=单价*数量
     */
    public void calTotal() {
        double total = 0;
        // 需要对数组进行遍历
        for (OrderItem o : items
             ) {
            total += o.getPprice() * o.getNum();
        }
        this.total = total;
    }
}
